package com.breakfast.fun.ui.user.address;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.dao.Address;
import com.breakfast.fun.dao.AddressDao;
import com.breakfast.fun.dao.DaoMaster;
import com.breakfast.fun.data.entity.user.address.AddressData;
import com.breakfast.fun.data.entity.user.address.BuildingData;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.PopUtil;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.CustomDialog;
import com.breakfast.fun.view.widget.TitleBarView;
import com.breakfast.fun.view.widget.WheelPicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;

public class EditAddressActivity extends BaseActivity<EditAddressPresenter, EditAddressModel> implements EditAddressContract.View {

    @Bind(R.id.tbv_edit_address)
    TitleBarView mTbvEditAddress;
    @Bind(R.id.et_edit_address_name)
    EditText mEtEditAddressName;
    @Bind(R.id.et_edit_address_mobile)
    EditText mEtEditAddressMobile;
    @Bind(R.id.tv_edit_address_building)
    TextView mTvEditAddressBuilding;
    @Bind(R.id.tv_edit_address_floor)
    TextView mTvEditAddressFloor;
    @Bind(R.id.et_edit_address_detail)
    EditText mEtEditAddressDetail;
    @Bind(R.id.btn_edit_address)
    Button mBtnEditAddress;
    @Bind(R.id.ll_edit_address)
    LinearLayout mLlEditAddress;

    private CustomDialog mDialog;
    private BuildingData mBuildingData;
    private AddressDao mAddressDao;

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_address;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mTbvEditAddress.setAll("编辑地址");
        mTbvEditAddress.setRightImg(R.drawable.ic_recover);
        mTbvEditAddress.setBack(this);

        AddressData data = (AddressData) getIntent().getSerializableExtra("address");
        mEtEditAddressName.setText(data.getConsignee());
        mEtEditAddressMobile.setText(data.getPhone());
        mEtEditAddressDetail.setText(data.getDetail());
        mTvEditAddressBuilding.setText(data.getBuilderName());
        mTvEditAddressFloor.setText(data.getFloorNo());

        mAddressDao = DaoMaster.newDevSession(context, "zaofan.db").getAddressDao();

        Address address = mAddressDao.loadByRowId(1);

        mTbvEditAddress.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog = new CustomDialog(EditAddressActivity.this, R.style.dialog_style,
                        R.layout.dialog_custom, "提示", "确定要删除该地址吗？", false, false, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mDialog.dismiss();
                        //删除addressDao中，购物车缓存的地址
                        if(null != address) {
                            if (data.getId() == address.getAddressId()) {
                                mAddressDao.delete(address);
                            }
                        }
                        presenter.deleteAddress(data.getId() + "");
                    }
                });
                mDialog.show();
            }
        });

        mTvEditAddressFloor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopPicker();
            }
        });

        mTvEditAddressBuilding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditAddressActivity.this, ChooseBuildingActivity.class);
                startActivity(intent);
            }
        });

        mBtnEditAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtEditAddressMobile.getText().toString())
                        || mEtEditAddressMobile.getText().toString().length() != 11) {
                    T.show("请检查手机号格式");
                    return;
                }
                if (TextUtils.isEmpty(mEtEditAddressName.getText().toString())) {
                    T.show("姓名不能为空");
                    return;
                }
                if (mTvEditAddressBuilding.equals("请选择送餐大楼")) {
                    T.show("请选择送餐大楼");
                    return;
                }
                if (mTvEditAddressFloor.equals("请选择公司楼层")) {
                    T.show("请选择公司楼层");
                    return;
                }
                AddressData addressData = new AddressData();
                addressData.setConsignee(mEtEditAddressName.getText().toString());
                addressData.setPhone(mEtEditAddressMobile.getText().toString());
                if (mBuildingData != null) {
                    addressData.setBuilderId(mBuildingData.getId());
                    addressData.setBuilderName(mBuildingData.getBuilderName());
                } else {
                    addressData.setBuilderId(data.getBuilderId());
                    addressData.setBuilderName(data.getBuilderName());
                }
                addressData.setId(data.getId());
                addressData.setFloorNo(mTvEditAddressFloor.getText().toString());
                addressData.setDetail(mEtEditAddressDetail.getText().toString());
                presenter.editAddress(addressData);
            }
        });

    }

    private void showPopPicker() {
        //popwindow要装载的view
        View contentView = LayoutInflater.from(context).inflate(R.layout.widget_pop_picker, null);

        TextView tvWheelpickerCancel = (TextView) contentView.findViewById(R.id.tv_wheelpicker_cancel);
        TextView tvWheelpickerOk = (TextView) contentView.findViewById(R.id.tv_wheelpicker_ok);
        final WheelPicker wpWheelPicker = (WheelPicker) contentView.findViewById(R.id.wp_wheelpicker);

        final PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        PopUtil.setPop(popupWindow);

        popupWindow.showAtLocation(EditAddressActivity.this.findViewById(R.id.ll_edit_address), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });


        tvWheelpickerOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String floor = (String) wpWheelPicker.getData().get(wpWheelPicker.getCurrentItemPosition());
                mTvEditAddressFloor.setText(floor);
                popupWindow.dismiss();
            }
        });

        tvWheelpickerCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBuilding(BuildingData data) {
        presenter.chooseBuilding(data);
    }

    @Override
    public void setBuilding(BuildingData buildingData) {
        this.mBuildingData = buildingData;
        mTvEditAddressBuilding.setText(buildingData.getBuilderName());
    }

    @Override
    public void showSuccess(String msg) {
        T.show(msg);

        finish();
        EventBus.getDefault().post(new RefreshEvent("UserAddressActivity"));
    }

    @Override
    protected void destroyView() {
        super.destroyView();
        EventBus.getDefault().unregister(this);
    }
}
